<?php
/**
 * 单据管理
 * @author tangbl
 *
 */
class BillAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 生成单据
     * @access public
     * @param  array  $map      应用参数(company,outletid,addid,addno)   
     * 
     * @param  String $pid      商品id(14,15)为空则找到订单详情商品
     * @param  int    $oldid    订单id
     * @param  int    $type     单据类型[1发货单|2补货单|退货单]
     * @param  String $content  备注
     */
    public function create(){
    	
    	$oldid = $this->_req("oldid","trim","");
    	$pid   = $this->_req("pid");
    	if($pid && !is_array($pid)){
    		$pid = explode(",", $pid);
    	}

    	$model = D("Order");
    	$where = array("id|orderno"=>$oldid);
    	$order = $model->where($where)->find();
    	
    	if($order['id']){
    		
    		unset($order['id']);
    		$orderno = $order['orderno'];
    		$dealno  = createOrderNo();
    		$time    = time();
    		$map     = parent::getParams();
    		$order   = array_merge($order,$map);
    		
    		$order['type']     = $this->_req("type"   ,"intval",1);
    		$order['content']  = $this->_req("content","trim","");
    		$order['addtime']  = time();
    		$order['oldid']    = $oldid;
    		$order['dealno']   = $dealno;
    		$billid = M("Bill")->add($order);
    		
    		$m['orderno'] = $orderno;
    		if($pid){
    			$m['pid'] = array("in",$pid);
    		}
    		$detail  = M("OrderDetail")->where($m)->select();
	
    		$module  = M("BillDetail");
    		foreach($detail as $v){
    			unset($v['id']);
    			$v['billid'] = $billid;
    			$v['dealno'] = $dealno;
    			$module->add($v);
    		}
    		$this->success($billid);
    	}else{
    		$this->error();
    	}
    	
    }
    
    /**
     * 分配单据到用户
     * @access public
     * @param  array  $map      应用参数(company,outletid,addid,addno)  
     * 
     * @param  int    $billid   单据id  required
     * @param  int    $userid   旧用户id 
     * @param  int    $adminid  新用户id required
     * @param  String $content  操作说明
     * @return array
     */
    public function allotUser(){
    	 
    	$map = parent::getParams();
    	
    	$map['billid']   = $this->_req("billid" ,"intval");
    	$map['adminid']  = $this->_req("adminid","intval");
    	$map['userid']   = $this->_req("userid" ,"intval");
    	$map['content']  = $this->_req("content","trim","");
    	
    	$model = D("Bill");
    	$id    = $model->allotUser($map);
    	if($id){
    		$this->success($id);
    	}else{
    		$this->error($id);
    	}
    }
    
    /**
     * 挂单列表
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     */
    public function lockList(){
    	
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['addid']   = $this->_req("addid" ,  "intval");
    	$map['type']    = 4;

    	$model = D("Bill");
    	$list  = $model->where($map)->order("addtime desc")->select();
    	$this->success($list);
    }
    
    /**
     * 挂单操作
     * @param  array  $map     应用参数(company,outletid,addid,addno)
	 * @param  String $tableid 用户模型
	 * @param  String $dataid  用户编号
	 * @param  String $content 备注
	 * 
     */
    public function lockOrder(){
    	
    	$param = array(
    		"arr"   => array("type"=>4,"dealno"=>createOrderNo()),
    		"after" => "order_lock_after"
    	);
    	parent::do_add("Bill", $param);
    }
    
    /**
     * 解挂操作
     * @param  array  $map    应用参数(company,outletid,addid,addno)
	 * @param  int    $billid 单据编号
     */
    public function unlockOrder(){
    	
    	$billid = $this->_req("billid", "intval", 0);
    	$model  = D("Bill");
    	$bill   = $model->unlockOrder($billid);
    	
    	if($bill){
    		$this->success($bill);
    	}else{
    		$this->error();
    	}
    }
}